//
//  LoginAppDelegate.m
//  Login
//
//  Created by FER on 22/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "LoginAppDelegate.h"
#import "ConnectionHTTP.h"



@implementation LoginAppDelegate

@synthesize window;
@synthesize tabBarController;


#pragma mark -
#pragma mark Application lifecycle



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	// Clicked the Submit button
	if (buttonIndex != [alertView cancelButtonIndex])
	{
		NSString *loginURL = [NSString stringWithFormat:@"ios/user.php?user=%@&passkey=%@",textfieldName.text,textfieldPassword.text];
		NSString *loginResponse= [ConnectionHTTP doRequest:(NSString *)loginURL];
		//NSLog(@"Full response: %@",loginResponse);
	
		//UserObject *user = [[UserObject alloc] init];
	
		//Parse XML
		xmlBuffer = [ [ loginResponse stringByReplacingOccurrencesOfString: @"<" withString :@"\n<" ] componentsSeparatedByCharactersInSet: [ NSCharacterSet newlineCharacterSet ] ];
		
		for(int i = 0; i < [ xmlBuffer count ]; i++) 
		{
			NSString *line = [ xmlBuffer objectAtIndex: i ];
			if ([ line hasPrefix: @"<code>" ]) 
			{
				[UserObject setCode:(int)[[line stringByReplacingOccurrencesOfString: @"<code>" withString: @"" ] intValue]];
				
			}
			
			if ([ line hasPrefix: @"<userName>" ]) 
			{
				[UserObject setName:(NSString *)[ line stringByReplacingOccurrencesOfString: @"<userName>" withString: @"" ]];
				
			}
			
			if ([ line hasPrefix: @"<userLastName>" ]) 
			{
				[UserObject setLastName:(NSString *)[ line stringByReplacingOccurrencesOfString: @"<userLastName>" withString: @"" ]];

			}			

		}
		if ([UserObject getCode ] == 1) {
			[self someMethod];
		}
		else {
			//Se cargo bien
			NSArray *views= self.tabBarController.viewControllers;
			NSLog (@"Number of elements in array = %i", [views count]);
			
			UIViewController *actual= [views objectAtIndex: 0];
			
			}
	
	}
	else 
	{
		[self someMethod];
	}

}


- (void) someMethod
{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please Login" message:@""
												   delegate:self cancelButtonTitle:@"Cancel"  otherButtonTitles:@"Submit", nil];
	
	[alert addTextFieldWithValue:@"" label:@"User Name"];
	[alert addTextFieldWithValue:@"" label:@"Password"];
	
	// Username
	textfieldName = [alert textFieldAtIndex:0];
	textfieldName.keyboardType = UIKeyboardTypeAlphabet;
	textfieldName.keyboardAppearance = UIKeyboardAppearanceAlert;
	textfieldName.autocorrectionType = UITextAutocorrectionTypeNo;
	
	// Password
	textfieldPassword = [alert textFieldAtIndex:1];
	textfieldPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
	textfieldPassword.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
	textfieldPassword.keyboardAppearance = UIKeyboardAppearanceAlert;
	textfieldPassword.autocorrectionType = UITextAutocorrectionTypeNo;
	textfieldPassword.secureTextEntry = YES;
	
	[alert show];
}







- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after app launch.
    
	// Set the tab bar controller as the window's root view controller and display.
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    
	[self someMethod];
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive.
     */
}


- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     */
}


#pragma mark -
#pragma mark UITabBarControllerDelegate methods

/*
 // Optional UITabBarControllerDelegate method
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
}
 */

/*
 // Optional UITabBarControllerDelegate method
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
}
 */

#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    /*
     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
     */
}


- (void)dealloc {
    [tabBarController release];
    [window release];
    [super dealloc];
}

@end

